/*********************************************************************************
 *      Copyright:  (C) 2022 Zhang Changxing<ZhangChangxingVIP@163.com>
 *                  All rights reserved.
 *
 *       Filename:  serial_init.h
 *    Description:  This file is used for function declarations.
 *                 
 *        Version:  1.0.0(05/1/2022)
 *         Author:  Zhang Changxing <ZhangChangxingVIP@163.com>
 *      ChangeLog:  1, Release initial version on "05/1/2022  01:33:25 AM"
 *                 
 ********************************************************************************/



#ifndef _UART_INIT_H_
#define _UART_INIT_H_

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <termios.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <time.h>
#include "logger.h"


#define SERIAL_NAME 128


//Sets the context for the serial port properties
typedef struct ttyusb_ctx_s{
    int     fd;                         //Serial port file descriptor
    int     baudrate;                   //Baud rate
    int     databits;                   //Data bits
    char    parity;                     //The parity bit
    int     stopbits;                   //Stop bits
    char    serial_name[SERIAL_NAME];   //Name of serial port device
    int     msend_len;                  //Maximum sending length at a time
    int     timeout;                    //Timeout period for receiving messages
    struct  termios old_termios;        //Store the original properties of the serial port
}ttyusb_ctx_t;



/*--------------------------------------------------------------
 *   description:   Open the serial port File
 *          args:   $ttyusb_ctx:    Serial port property context
 *  return value:   <0: failure,    =0:ok 
 *--------------------------------------------------------------*/
int tty_open(ttyusb_ctx_t *ttyusb_ctx);


/*--------------------------------------------------------------
 *   description:   Closing the serial port File
 *          args:   $ttyusb_ctx:    Serial port property context
 *  return value:   <0: failure,    =0:ok 
 *--------------------------------------------------------------*/
int tty_close(ttyusb_ctx_t *ttyusb_ctx);


/*--------------------------------------------------------------
 *   description:   Example Initialize the serial port properties
 *          args:   $ttyusb_ctx:    Serial port property context
 *  return value:   <0: failure,    =0:ok 
 *--------------------------------------------------------------*/
int tty_init(ttyusb_ctx_t *ttyusb_ctx);


/*--------------------------------------------------------------
 *   description:   Sending data to the serial port
 *          args:   $ttyusb_ctx:    Serial port property context
 *                  $send_buf       Buffer address for data to be sent
 *                  $sbuf_len       The length of the send_buf
 *  return value:   <0: failure,    =0:ok 
 *--------------------------------------------------------------*/
int tty_send(ttyusb_ctx_t *ttyusb_ctx, char *send_buf, int sbuf_len);


/*--------------------------------------------------------------
 *   description:   Receive data from the serial port
 *          args:   $ttyusb_ctx:    Serial port property context
 *                  $recv_buf       Buffer address for data to be receive
 *                  $rbuf_len       The length of the recv_buf
 *  return value:   <0: failure,    =0:ok 
 *--------------------------------------------------------------*/
int tty_recv(ttyusb_ctx_t *ttyusb_ctx, char *recv_buf, int rbuf_len);

#endif